<?php

/*
Zoo Management Simulation
This file edits the database by updating the entry table or deleting an entry record based on user actions on a previous file
*/

include("dbconnect.php");
if ($_REQUEST['submit'] === "Return to Administator page"){
	header('Location: setupAdmin.php');
}

$numEntries = $_REQUEST['numentry'];
$entrynumber = 1;
$cageid = $_REQUEST['cageid'];
$entryid = $_REQUEST['entryid'];

if ($_REQUEST['submit'] === "Delete"){
	$deleteentrysql = "DELETE FROM `entry` WHERE entryId=".$entryid;
	$updateenclosuresql = "UPDATE `enclosure` SET `numEnclosureEntries`=".($numEntries-1)."WHERE entryId=".$entryid;
	$dbh->exec($deleteentrysql);
	$dbh->exec($updateenclosuresql);
}

else if ($_REQUEST['submit'] === "Edit Cage Entries"){
$idSql = "SELECT entryId FROM entry WHERE enclosureId=".$cageid;
foreach ($dbh->query($idSql) as $row)
	{
		$id = $row[0];
		$doortype = $_REQUEST["doortype".$entrynumber];
		$doorstatus = $_REQUEST["doorstatus".$entrynumber];
		$entrysql = "UPDATE `entry` SET `entryTypeId`=".$doortype.",`isEntryOpen`=".$doorstatus." WHERE entryId=".$id;
		$dbh->exec($entrysql);
		$entrynumber+=1;
	}
}
$dbh = null;
header('Location: index.php');
?>